<?php
class ModuleAction extends BaseAction {
    
	//模块中心页
	//用途：显示
	public function index(){
		
		$catalogUrlName = $this->_get("catalog_url_name");
		$current_catalog = CatalogHelper::getObject("catalog_url_name='".$catalogUrlName."'");
		if(empty($current_catalog)){
			header("HTTP/1.0 404 Not Found");    //加入404状态，让搜索引擎识别
			$this->display('Public:404');
			return;
		}
		if(empty($current_catalog["seo_title"])){
			$current_catalog["seo_title"] = $current_catalog["catalog_name"];
		}
		if(empty($current_catalog["seo_keywords"])){
			$current_catalog["seo_keywords"] = $current_catalog["catalog_name"];
		}
		
		//网站基本配置
		$config = WebConfigHelper::getObject();
		$config["footer_cont"] = htmlspecialchars_decode($config["footer_cont"]);
		$config = CommonUtil::appendCustomerJSconfig($config);	//合并[脚本JS代码 & 客服JS代码]
		
		$location = CatalogHelper::getLocation($current_catalog);	//当前页面导航位置
		$root_catalog = array();
		if($current_catalog["root_id"] != 0){	//根节点ID不为0，则取出根栏目
			$root_catalog = CatalogHelper::getObject("catalog_id=".$current_catalog["root_id"]);
		}else{
			$root_catalog = $current_catalog;
		}
		$root_cata_id = $root_catalog["catalog_id"];
		$current_cata_id = $current_catalog["catalog_id"];
		
		//---------------导航位置变量开始--------------------------------------------
		$this->assign("current_cata_id",$current_cata_id);
		$this->assign("root_cata_id",$root_cata_id);
		$this->assign("current_catalog",$current_catalog);
		$this->assign("root_catalog",$root_catalog);
		$this->assign("location",$location);
		//---------------导航位置变量结束--------------------------------------------
		
		$module_mapping_filepath = $current_catalog["view_module_filepath"];	//模块指向的模版
		
		//SEO数据
		$seo = array();
		$company_name = "";
		if(!empty($config["company_name"])){
			$company_name = "_".$config["company_name"];
		}
		$seo["title"] = $current_catalog["seo_title"].$company_name;
		$seo["keywords"] = $current_catalog["seo_keywords"];
		$seo["description"] = $current_catalog["seo_description"];
		
		if($current_catalog["catalog_type"] == CatalogType::$SINGEL_ARTICLE){	//单篇模块
			$article = SingelArticleHelper::getObject("catalog_id=".$current_catalog["catalog_id"]);
			$article["content"] = htmlspecialchars_decode($article['content']);
			
			$company_name = "";
			if(!empty($config["company_name"])){
				$company_name = "_".$config["company_name"];
			}
			
			$this->assign("info",$article);
			
		}elseif ($current_catalog["catalog_type"] == CatalogType::$DYNAMIC_MODULE){	//动态模块
			
			$dynamicModule = DynamicModuleHelper::getObject("dynamic_module_id=".$current_catalog["dynamic_module_id"]);
			if(empty($dynamicModule)){
				header("HTTP/1.0 404 Not Found");
				$this->display('Public:404');
				exit();
			}
			
			$module_mapping_filepath = $current_catalog["list_module_filepath"];
			$tableName = C("DB_PREFIX").$dynamicModule["table_name"];
			
			//查询该页是否有[同类型]的子子孙孙类
			$catalogIdWhereSql = "({$tableName}.catalog_id=".$current_catalog["catalog_id"];
			$str = CatalogHelper::getSubCataStr($current_catalog);
			if(!empty($str)){
				$subCataList = explode(",", $str);
				foreach ($subCataList as $id){
					$catalogIdWhereSql .= " OR {$tableName}.catalog_id={$id}";
				}
			}
			$catalogIdWhereSql .= ")";
			
			$page = $this->_get("page");
			$page = $page>1? $page : 1;	//如page参数为[字母]或[为空]，情况的默认赋值
			
			$sql = "SELECT * FROM {$tableName} LEFT JOIN ".C("DB_PREFIX")."catalog ON {$tableName}.catalog_id=".C("DB_PREFIX")."catalog.catalog_id WHERE ".$catalogIdWhereSql." ORDER BY timeline DESC";	//分页查询SQL语句
			$pagination = CommonUtil::getPage($sql,$page,$current_catalog["page_show_num"]);
			foreach ($pagination["resultList"] as &$obj){
				if(!empty($obj["color"])){
					$obj["title"] = '<span style="color:'.$obj["color"].'">'.$obj["title"].'</span>';
				}
				$obj["link"] = LinkUtil::getLink($obj["id"], $obj["catalog_id"]);
			}
			$this->assign("pagination",$pagination);
		}elseif ($current_catalog["catalog_type"] == CatalogType::$FM_PAGE){	//封面页模块
			//TODO 空的，具体的内容，直接显示用户自定义的模版
		}elseif ($current_catalog["catalog_type"] == CatalogType::$GUESTBOOK){	//留言板模块
			
			$page = $this->_get("page");
			$page = $page>1? $page : 1;	//如page参数为[字母]或[为空]，情况的默认赋值
			
			$tableName = C("DB_PREFIX")."guestbook";
			$sql = "SELECT * FROM {$tableName} WHERE catalog_id={$current_cata_id} AND reply IS NOT NULL ORDER BY timeline DESC";
			$pagination = CommonUtil::getPage($sql,$page,10);
			foreach ($pagination["resultList"] as &$obj){
				$obj["timeline"] = CommonUtil::formatDate($obj["timeline"]);
			}
			
			$module_mapping_filepath = "guestbook.html";
			$this->assign("form_url",__APP__."/guestbook/add/?catalog_id=".$current_cata_id);
			$this->assign("verifycode",__APP__."/verifycode/");
			$this->assign("pagination",$pagination);
			
		}elseif ($current_catalog["catalog_type"] == CatalogType::$RESERVE_MODULE){	//表单模块
			
			$field_list = ReserveModuleFieldHelper::getList("reserve_module_id={$current_catalog["dynamic_module_id"]} AND is_system=0","order_num DESC");
			foreach ($field_list as &$obj){
				$val = '<input name="'.$obj["field_name"].'" type="text" />';
				if($obj["form_type"] == FormType::$INPUT){
					$val = '<input name="'.$obj["field_name"].'" type="text" />';
				}elseif($obj["form_type"] == FormType::$TEXTAREA){
					$val = '<textarea name="'.$obj["field_name"].'"></textarea>';
				}elseif($obj["form_type"] == FormType::$NUMBER){
					$val = '<input name="'.$obj["field_name"].'" type="text" />';
				}elseif($obj["form_type"] == FormType::$SELECT){
					$arr = explode(",",$obj["select_val"]);
					$val = '<select name="'.$obj["field_name"].'">';
					foreach ($arr as $optionVal){
						$val .= '<option value="'.$optionVal.'">'.$optionVal.'</option>';
					}
					$val .= '</select>';
				}elseif($obj["form_type"] == FormType::$FILE){
					$val = '<input type="file" name="'.$obj["field_name"].'" />';
				}
				$obj["form_val"] = $val;
				
			}
			$this->assign("field_list",$field_list);
			$this->assign("form_url",__APP__."/reserve/add/?catalog_id=".$current_cata_id);
			$this->assign("verifycode",__APP__."/verifycode/");
		}
		
		$this->assign("config",$config);
		$this->assign("template_path",CommonUtil::getDir()."/themes/".$config["template_name"]);
		$this->assign("seo",$seo);
		
		$this->display(CommonUtil::getTemplatePath().$module_mapping_filepath);
		
    }
    
}